using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using QFramework;
using UnityEngine;

namespace HareGame
{
    [TaskCategory("引导")]
    public class Move2Pos : Action
    {
        [Header("目标对象")]
        public SharedGameObject targetObj;

        [Header("目标对象的名字(targetObj为空时生效)")]
        public string targetName;

        [Header("偏移值")]
        public Vector3 offset;

        [Header("相机广角")]
        public float size = 20;

        [Header("相机移动到位置后发送的事件(不支持参数)")]
        public string onReachEventKey;

        private bool _isReach = false;

        public override void OnStart()
        {
            base.OnStart();

            _isReach = false;
            if (targetObj.Value == null)
            {
                targetObj.Value = GameObject.Find(targetName);
                if (targetObj.Value == null)
                {
                    LogKit.E("找不到名为:" + targetName + "的对象");
                    return;
                }
            }
            Vector3 targetPos = new Vector3(targetObj.Value.transform.position.x, 75, targetObj.Value.transform.position.z) + offset;
            GameEntry.Camera.Move2Pos(targetPos, size, () =>
             {
                 if (!string.IsNullOrEmpty(onReachEventKey))
                 {
                     GameEntry.Event.Send(onReachEventKey);
                 }
                 _isReach = true;
             });
        }

        public override TaskStatus OnUpdate()
        {
            return _isReach ? TaskStatus.Success : TaskStatus.Running;
        }
    }
}